package com.ztgf.shopping.api.client.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopShoppingCartBO;
import com.ztgf.shopping.models.bo.shop.ShopShoppingCartSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopShoppingCartDTO;
import com.ztgf.shopping.models.service.shop.IShopShoppingCartService;
import com.ztgf.shopping.models.vo.shop.ShopCommodityCartVO;
import com.ztgf.shopping.models.vo.shop.ShopShoppingCartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.getIds;

/**
 * 用户购物车表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-27 14:05:12
 */

@RestController
@RequestMapping("/api/shopShoppingCart")
@Slf4j
@Api(value = "ShopShoppingCartApiController", tags = "用户购物车表Api接口")
public class ShopShoppingCartApiController {
    @DubboReference
    private IShopShoppingCartService shopShoppingCartService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<ShopShoppingCartDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        ShopShoppingCartDTO dto = shopShoppingCartService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "添加用户购物车表", notes = "添加用户购物车表")
    @PostMapping(value = "/insert")
    public Result insert(ShopShoppingCartSaveBO saveBO) {
        if (saveBO.getShopId() == null || saveBO.getCommodityId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        boolean flag = shopShoppingCartService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "批量添加用户购物车表", notes = "批量添加用户购物车表")
    @PostMapping(value = "/insertBatch")
    public Result insertBatch(String saveBOs) {
        if (StringUtils.isEmpty(saveBOs)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = shopShoppingCartService.addBatch(saveBOs);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "再次下单", notes = "再次下单")
    @PostMapping(value = "/addToCartByOrderId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单id",required = true,paramType = "query", dataType = "Long")
    })
    public Result addToCartByOrderId(Long orderId) {
        if (orderId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = shopShoppingCartService.addToCartByOrderId(orderId);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "更新用户购物车表", notes = "更新用户购物车表")
    @PostMapping(value = "/update")
    public Result update(ShopShoppingCartSaveBO saveBO) {
        if (saveBO.getId() == null) {
            return new Result<>(CodeEnum.FAIL);
        }

        ShopShoppingCartDTO dto = new ShopShoppingCartDTO();
        if (saveBO.getPrice() != null) {
            dto.setPrice(saveBO.getPrice());
        }
        if (saveBO.getNumber() != null) {
            dto.setNumber(saveBO.getNumber());
        }
        if (saveBO.getSpecificationIds() != null) {
            String ids = getIds(saveBO.getSpecificationIds());
            dto.setSpecificationIds(ids);
        }
        dto.setId(saveBO.getId());

        boolean flag = shopShoppingCartService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @ApiOperation(value = "删除用户购物车表", notes = "删除用户购物车表")
    @ApiImplicitParam(name = "id", value = "用户购物车表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = shopShoppingCartService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除用户购物车表", notes = "批量删除用户购物车表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        shopShoppingCartService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @ApiOperation(value = "用户购物车表列表", notes = "用户购物车表列表")
    @PostMapping("/queryPageList")
    public Result<ShopShoppingCartVO> queryPageList(ShopShoppingCartBO bo) {
        ShopShoppingCartVO shopShoppingCartVO = shopShoppingCartService.queryShoppingCartPageList(bo);

        return new Result<>(shopShoppingCartVO);
    }

    @ApiOperation(value = "首页用户购物车表列表", notes = "首页用户购物车表列表")
    @PostMapping("/queryHomePageList")
    public Result<List<ShopCommodityCartVO>> queryHomePageList(ShopShoppingCartBO bo) {
        List<ShopCommodityCartVO> ShopCommodityCartVO = shopShoppingCartService.queryHomePageList(bo);
        return new Result<>(ShopCommodityCartVO);
    }

    @ApiOperation(value = "购物车总数量", notes = "购物车总数量")
    @PostMapping("/queryCount")
    public Result<Integer> queryCount(ShopShoppingCartBO bo) {
        Wrapper<ShopShoppingCartDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", bo.getShopId());
        Integer count = shopShoppingCartService.selectCount(wrapper);
        return new Result<>(count);
    }
}
